package cn.xhy.module.purchase.controller.admin.apply;
import cn.xhy.module.purchase.dal.dataobject.materialdemand.ProjectOrderDO;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.xhy.module.purchase.controller.admin.apply .vo.*;
import cn.xhy.module.purchase.service.apply .ProjectOrderService;

@Tag(name = "管理后台 - 项目物资申请单")
@RestController
@RequestMapping("/purchase/project-order")
@Validated
public class ProjectOrderController {

    @Resource
    private ProjectOrderService projectOrderService;

    @PostMapping("/create")
    @Operation(summary = "创建项目物资申请单")
    @PreAuthorize("@ss.hasPermission('purchase:project-order:create')")
    public CommonResult<Long> createProjectOrder(@Valid @RequestBody ProjectOrderSaveReqVO createReqVO) {
        return success(projectOrderService.createProjectOrder(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新项目物资申请单")
    @PreAuthorize("@ss.hasPermission('purchase:project-order:update')")
    public CommonResult<Boolean> updateProjectOrder(@Valid @RequestBody ProjectOrderSaveReqVO updateReqVO) {
        projectOrderService.updateProjectOrder(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除项目物资申请单")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('purchase:project-order:delete')")
    public CommonResult<Boolean> deleteProjectOrder(@RequestParam("id") Long id) {
        projectOrderService.deleteProjectOrder(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得项目物资申请单")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('purchase:project-order:query')")
    public CommonResult<ProjectOrderRespVO> getProjectOrder(@RequestParam("id") Long id) {
        ProjectOrderDO projectOrder = projectOrderService.getProjectOrder(id);
        return success(BeanUtils.toBean(projectOrder, ProjectOrderRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得项目物资申请单分页")
    @PreAuthorize("@ss.hasPermission('purchase:project-order:query')")
    public CommonResult<PageResult<ProjectOrderRespVO>> getProjectOrderPage(@Valid ProjectOrderPageReqVO pageReqVO) {
        PageResult<ProjectOrderDO> pageResult = projectOrderService.getProjectOrderPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ProjectOrderRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出项目物资申请单 Excel")
    @PreAuthorize("@ss.hasPermission('purchase:project-order:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportProjectOrderExcel(@Valid ProjectOrderPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ProjectOrderDO> list = projectOrderService.getProjectOrderPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "项目物资申请单.xls", "数据", ProjectOrderRespVO.class,
                        BeanUtils.toBean(list, ProjectOrderRespVO.class));
    }

}
